package com.qy.controller.app;

import com.qy.base.core.*;
import com.qy.base.utils.DateUtil;
import com.qy.model.Enshrine;
import com.qy.model.Member;
import com.qy.service.EnshrineService;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zaq on 2018/07/10.
 */
@RestController
@RequestMapping("app/enshrine")
public class EnshrineController {
    @Resource
    private EnshrineService enshrineService;

    @PostMapping("/add")
    public Result add(@RequestBody Enshrine enshrine) {
        enshrineService.save(enshrine);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        enshrineService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Enshrine enshrine) {
        enshrineService.update(enshrine);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Enshrine enshrine = enshrineService.findById(id);
        return ResultGenerator.successResult(enshrine);
    }

    @GetMapping("/list")
    public Result list(PageBean<Enshrine> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<Enshrine> list = enshrineService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //收藏
    @PostMapping("/enshrine")
    public Result enshrine(@RequestBody Enshrine enshrine) {
        if (enshrine.getUser_id() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        Condition condition = new Condition(Enshrine.class);
        Example.Criteria criteria = condition.createCriteria();
        if (enshrine.getPolicy_id() != null) {
            criteria.andCondition("user_id =" + enshrine.getUser_id());
            criteria.andCondition("policy_id =" + enshrine.getPolicy_id());
            List<Enshrine> enshrineList = enshrineService.findByCondition(condition);
            if (enshrineList.size() != 0) {
                throw new ServiceException(Constants.CODE_ENSHRINE);
            }
        } else if (enshrine.getUnit_id() != null) {
            criteria.andCondition("user_id =" + enshrine.getUser_id());
            criteria.andCondition("unit_id =" + enshrine.getUnit_id());
            List<Enshrine> enshrineList = enshrineService.findByCondition(condition);
            if (enshrineList.size() != 0) {
                throw new ServiceException(Constants.CODE_ENSHRINE);
            }
        } else if (enshrine.getCharge_id() != null) {
            criteria.andCondition("user_id =" + enshrine.getUser_id());
            criteria.andCondition("charge_id =" + enshrine.getCharge_id());
            List<Enshrine> enshrineList = enshrineService.findByCondition(condition);
            if (enshrineList.size() != 0) {
                throw new ServiceException(Constants.CODE_ENSHRINE);
            }
        } else {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        enshrine.setAdd_time(DateUtil.getNowTimestamp());
        enshrineService.save(enshrine);
        return ResultGenerator.successResult();
    }

    //取消收藏
    @PostMapping("/cancelEnshrine")
    public Result cancelEnshrine(@RequestBody Enshrine enshrine) {
        if (enshrine.getUser_id() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        Condition condition = new Condition(Enshrine.class);
        Example.Criteria criteria = condition.createCriteria();
        if (enshrine.getPolicy_id() != null) {
            criteria.andCondition("user_id =" + enshrine.getUser_id());
            criteria.andCondition("policy_id =" + enshrine.getPolicy_id());
            List<Enshrine> enshrineList = enshrineService.findByCondition(condition);
            if (enshrineList.size() == 0) {
                throw new ServiceException(Constants.CODE_ERR_ENSHRINE);
            }
            enshrineService.deleteById(enshrineList.get(0).getId());
        } else if (enshrine.getUnit_id() != null) {
            criteria.andCondition("user_id =" + enshrine.getUser_id());
            criteria.andCondition("unit_id =" + enshrine.getUnit_id());
            List<Enshrine> enshrineList = enshrineService.findByCondition(condition);
            if (enshrineList.size() == 0) {
                throw new ServiceException(Constants.CODE_ERR_ENSHRINE);
            }
            enshrineService.deleteById(enshrineList.get(0).getId());
        } else if (enshrine.getCharge_id() != null) {
            criteria.andCondition("user_id =" + enshrine.getUser_id());
            criteria.andCondition("charge_id =" + enshrine.getCharge_id());
            List<Enshrine> enshrineList = enshrineService.findByCondition(condition);
            if (enshrineList.size() == 0) {
                throw new ServiceException(Constants.CODE_ERR_ENSHRINE);
            }
            enshrineService.deleteById(enshrineList.get(0).getId());
        }
        return ResultGenerator.successResult();

    }

    //收藏数量
    @GetMapping("/enshrineNumber")
    public Result policyNumber(Enshrine enshrine) {
        if (enshrine.getUser_id() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        List<Map> list1 = new ArrayList<>();
        List<Map> list2 = new ArrayList<>();
        List<Map> list3 = new ArrayList<>();

        list1 = enshrineService.selectPolicyEnshrine(enshrine);
        list2 = enshrineService.selectUnitEnshrine(enshrine);
        list3 = enshrineService.selectChargeEnshrine(enshrine);

        Map<String, Object> map = new HashMap<>();
        map.put("policyNumber", list1.size());
        map.put("unitNumber", list2.size());
        map.put("chargeNumber", list3.size());

        return ResultGenerator.successResult(map);
    }

    //我的收藏
    @GetMapping("/myEnshrine")
    public Result myEnshrine(PageBean<Map> page, Enshrine enshrine) {
        if (enshrine.getUser_id() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        List<Map> list = new ArrayList<>();
        if (enshrine.getType() == 1) {
            PageHelper.startPage(page.getPageNum(), page.getSize(), "add_time desc");
            list = enshrineService.selectPolicyEnshrine(enshrine);
        } else if (enshrine.getType() == 2) {
            PageHelper.startPage(page.getPageNum(), page.getSize(), "add_time desc");
            list = enshrineService.selectUnitEnshrine(enshrine);
        } else if (enshrine.getType() == 3) {
            PageHelper.startPage(page.getPageNum(), page.getSize(), "add_time desc");
            list = enshrineService.selectChargeEnshrine(enshrine);
        }
        page.setList(list);
        return ResultGenerator.successResult(page);
    }


}
